﻿
using QLKS2014.BLL;
using QLKS2014.PUBLIC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace QLKS2014.GUI.StupidWindow
{
    /// <summary>
    /// Đăng nhập vào hệ thống
    /// </summary>
    public partial class LoginWindow : Window
    {
        MainWindow _mainWindow;
        public LoginWindow(MainWindow mainWindow_)
        {
            InitializeComponent();
            this._mainWindow = mainWindow_;
            Loaded += new RoutedEventHandler(LogInWindow_Loaded);
            Closing += LogInWindow_Closing;
        }


        #region Event Handle


        private void LogInWindow_Loaded(object sender, RoutedEventArgs e)
        {
            _initialize();
        }

        private void _initialize()
        {
            _initializeForLogin();
        }

        void LogInWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MessageBoxResult msb = MessageBox.Show("Đóng toàn bộ chương trình",
                "Warnning ", MessageBoxButton.OK);
            _mainWindow.Close();
        }

        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            _logIn();
        }

        private void btnLogOut_Click(object sender, RoutedEventArgs e)
        {
            _logOut();
            
        }

        private void _logOut()
        {
            lblShowName.Content = "";
            _showLoginUserToWindow(false);
        }


        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


        private void LogInWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (isLogingIn())
            {
                if (e.Key == Key.Enter)
                {
                    _logIn();
                }
                else if (e.Key == Key.Escape)
                {
                    this.Close();
                }
            }
            else
            {
                if (e.Key == Key.Enter)
                {
                    _logOut();
                }
                else if (e.Key == Key.Escape)
                {
                    this.Hide();
                }
            }

        }

        private bool isLogingIn()
        {
            if (btnLogIn.Visibility == System.Windows.Visibility.Visible)
                return true;
            return false;
        }

        private void btnCancelLogOut_Click(object sender, RoutedEventArgs e)
        {
            this.Hide();
        }

        private void LoginWindow_Actived(object sender, EventArgs e)
        {
            _initializeForLogin();
        }



        #endregion





        private void _logIn()
        {
            String userName = txtbUserName.Text;
            String password = pwbPassword.Password;
            NguoiDungPublic nguoiDung0 = new NguoiDungPublic();
            nguoiDung0.TenNguoiDung = userName;
            nguoiDung0.MatKhau = password;


            Boolean checkUser = _checkPassword(nguoiDung0);
            if (checkUser)// Login Success
            {
                _showLoginUserToWindow(true);
                this.Hide();
                return;
            }

            // login fail
            MessageBox.Show(
                        "Sai tên người dùng hoặc mật khẩu",
                        "Error", MessageBoxButton.OK, MessageBoxImage.Error
                        );
            return;
        }


        // Change login window to logout window
        private void _showLoginUserToWindow(bool p)
        {
            if (p)
            {
                lblUserName.Visibility = System.Windows.Visibility.Hidden;
                lblPassWord.Visibility = System.Windows.Visibility.Hidden;
                txtbUserName.Visibility = System.Windows.Visibility.Hidden;
                pwbPassword.Visibility = System.Windows.Visibility.Hidden;
                btnExit.Visibility = System.Windows.Visibility.Hidden;
                btnLogIn.Visibility = System.Windows.Visibility.Hidden;
                btnLogOut.Visibility = System.Windows.Visibility.Visible;
                btnCancelLogOut.Visibility = System.Windows.Visibility.Visible;
                lblShowName.Visibility = System.Windows.Visibility.Visible;
                pwbPassword.Password = ""; 
                lblShowName.Content = "Chào "+ txtbUserName.Text;
            }
            else
            {
                lblUserName.Visibility = System.Windows.Visibility.Visible;
                lblPassWord.Visibility = System.Windows.Visibility.Visible;
                txtbUserName.Visibility = System.Windows.Visibility.Visible;
                pwbPassword.Visibility = System.Windows.Visibility.Visible;
                btnExit.Visibility = System.Windows.Visibility.Visible;
                btnLogIn.Visibility = System.Windows.Visibility.Visible;
                btnLogOut.Visibility = System.Windows.Visibility.Hidden;
                btnCancelLogOut.Visibility = System.Windows.Visibility.Hidden;
                lblShowName.Visibility = System.Windows.Visibility.Hidden;
            }


        }

        private bool _checkPassword(NguoiDungPublic nguoidung_)
        {
            List<NguoiDungPublic> lNguoiDung = new NguoiDungBll().selectByName(nguoidung_);
            if(lNguoiDung==null||lNguoiDung.Count==0)
                return false;

            NguoiDungPublic nguoiDung0 = lNguoiDung[0];
            bool match = nguoidung_.MatKhau.Equals(nguoiDung0.MatKhau);
            return match;
        }



        private void _initializeForLogin()
        {
            lblShowName.Visibility = System.Windows.Visibility.Collapsed;
            btnLogOut.Visibility = System.Windows.Visibility.Collapsed;
            btnCancelLogOut.Visibility = System.Windows.Visibility.Collapsed;
            txtbUserName.Focus();
        }



        //private void btnLogIn_Click(object sender, RoutedEventArgs e)
        //{
        //    //if (
        //    //    string.IsNullOrEmpty(this.tbxUserName.Text) ||
        //    //    string.IsNullOrEmpty(this.pwbxPassWord.Password)
        //    //    )
        //    //    return;
        //    //DataUser.userName = this.tbxUserName.Text;
        //    //Boolean temp = user.testAccount(this.pwbxPassWord.Password);
        //    //if (temp == false)
        //    //{
        //    //    MessageBox.Show(
        //    //        "Sai tên người dùng hoặc mật khẩu",
        //    //        "Error", MessageBoxButton.OK, MessageBoxImage.Error
        //    //        );
        //    //    return;
        //    //}
        //    //lbName.Visibility = System.Windows.Visibility.Hidden;
        //    //lbPassWord.Visibility = System.Windows.Visibility.Hidden;
        //    //tbxUserName.Visibility = System.Windows.Visibility.Hidden;
        //    //pwbxPassWord.Visibility = System.Windows.Visibility.Hidden;
        //    //btnCancle.Visibility = System.Windows.Visibility.Hidden;
        //    //btnLogIn.Visibility = System.Windows.Visibility.Hidden;
        //    //btnLogOut.Visibility = System.Windows.Visibility.Visible;
        //    //lbShowName.Visibility = System.Windows.Visibility.Visible;
        //    //lbShowName.Content = "Chào " + DataUser.userName;
        //    //this.Hide();
        //}

        //private void btnExit_Click(object sender, RoutedEventArgs e)
        //{
        //    Application.Current.Shutdown();
        //}

        //private void btnLogOut_Click(object sender, RoutedEventArgs e)
        //{
        //    lbName.Visibility = System.Windows.Visibility.Visible;
        //    lbPassWord.Visibility = System.Windows.Visibility.Visible;
        //    tbxUserName.Visibility = System.Windows.Visibility.Visible;
        //    pwbxPassWord.Visibility = System.Windows.Visibility.Visible;
        //    btnExit.Visibility = System.Windows.Visibility.Visible;
        //    btnLogIn.Visibility = System.Windows.Visibility.Visible;
        //    btnLogOut.Visibility = System.Windows.Visibility.Hidden;
        //    lbShowName.Visibility = System.Windows.Visibility.Hidden;
        //}

        //private void Window_KeyDown(object sender, KeyEventArgs e)
        //{
        //    //if (e.Key == Key.Enter)
        //    //{
        //    //    if (
        //    //        string.IsNullOrEmpty(this.tbxUserName.Text) ||
        //    //        string.IsNullOrEmpty(this.pwbxPassWord.Password)
        //    //        )
        //    //        return;
        //    //    DataUser.userName = this.tbxUserName.Text;
        //    //    Boolean temp = user.testAccount(this.pwbxPassWord.Password);
        //    //    if (temp == false)
        //    //    {
        //    //        MessageBox.Show(
        //    //            "Sai tên người dùng hoặc mật khẩu",
        //    //            "Error", MessageBoxButton.OK, MessageBoxImage.Error
        //    //            );
        //    //        return;
        //    //    }
        //    //    lbName.Visibility = System.Windows.Visibility.Hidden;
        //    //    lbPassWord.Visibility = System.Windows.Visibility.Hidden;
        //    //    tbxUserName.Visibility = System.Windows.Visibility.Hidden;
        //    //    pwbxPassWord.Visibility = System.Windows.Visibility.Hidden;
        //    //    btnCancle.Visibility = System.Windows.Visibility.Hidden;
        //    //    btnLogIn.Visibility = System.Windows.Visibility.Hidden;
        //    //    btnLogOut.Visibility = System.Windows.Visibility.Visible;
        //    //    lbShowName.Visibility = System.Windows.Visibility.Visible;
        //    //    lbShowName.Content = "Chào " + DataUser.userName;
        //    //    this.Hide();
        //    //}
        //}
    }
}
